import os
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Bar

# 数据目录
data_dir = '/root/eduhq/result/ads_visit_mode/'
# 背景图片
background_image = '/root/eduhq/images/img_2.png'
# 输出图表存放地址
output_path = '/root/eduhq/html/网站访客设备类型统计堆积柱形图.html'

# 读取设备访问次数数据
data_file = os.path.join(data_dir, 'visit_mode_data.csv')  # 替换为你的数据文件
data = pd.read_csv(data_file)

# 假设数据文件有设备类型和对应的访问次数列
device_types = data['DeviceType'].tolist()  # 设备类型
access_counts = data.drop(columns=['DeviceType']).to_dict(orient='list')  # 访问次数，按设备类型分列

# 创建堆积柱形图
bar_chart = Bar()

# 添加每种设备类型的访问次数数据
for mode, counts in access_counts.items():
    bar_chart.add(mode, device_types, counts)

# 设置全局选项
bar_chart.set_global_opts(
    title_opts=opts.TitleOpts(title="网站访客设备类型统计堆积柱形图"),
    xaxis_opts=opts.AxisOpts(name='设备类型'),
    yaxis_opts=opts.AxisOpts(name='访问次数'),
    tooltip_opts=opts.TooltipOpts(trigger="axis"),
)

# 渲染图表
bar_chart.render_background_img(background_image)
bar_chart.render(output_path)

print(f"图表已生成并保存到: {output_path}")
